<?php

namespace App\Repositories;

use App\Jobs\Fun;
use App\Models\NewsModel;
use App\Models\NewsPriseModel;
use App\Models\User\UserModel;
use App\Repositories\Admin\UserRepository;
use http\Env\Request;
use function MongoDB\BSON\toJSON;

class NewsRepository
{
    protected $newsModel;
    protected $newsPriseModel;

    public function __construct()
    {
        $this->newsModel = new NewsModel;
        $this->newsPriseModel = new NewsPriseModel;
    }

    /**
     * 资讯列表
     * @param array $cids
     * @return array
     */
    public function getNews($type=0,$uid=0)
    {
        $where = [];
        if($type){
            $where['cid'] = $uid;
        }
        $result = $this->newsModel
            ->where(['status'=>0])
            ->where($where)
            ->orderBy('created_at','desc')
            ->select(['id','title','thumb','content','cid','praise'])
            ->skip(0)
            ->take(10)
            ->get();
      //   print_r($result);//exit;
        if (!count($result)) { return array(); }
        foreach ($result as $v) {
            $userModel = new UserRepository();
           // $user = $userModel->getShowOne($v->cid);
            $user=UserModel::getinfo($v->cid);
            $v->uname = $user ? $user->name : '测试用户';
            $v->head= Fun::pImg($user->head);
           /* if ($v->head && file_exists(ltrim($v->head,'/'))) {
                $v->head = $user->head;
            } else {
                $v->head =UserModel::defaultHead;
            }*/
            /*if ($v->thumb && file_exists(ltrim($v->thumb,'/'))) {
                $v->thumb = $user->thumb;
            } else {
                $v->thumb = '';
            }*/
            $v->thumb=Fun::pImg($v->thumb);
            $v->intro = $v->content;
            if (mb_strlen($v->content)>20) {
                $v->intro = mb_substr($v->content,0,20).'...';
            }
        }
     //   print_r($result);exit;
        return $result;
    }

    /**
     * 资讯详情
     * @param array $cids
     * @return array
     */
    public function getNewsDetail($new_id=0)
    {
        $model = $this->newsModel
            ->where(['status'=>0,'id'=>$new_id])
            ->first(['title','thumb','content','cid','praise','created_at']);
        if(empty($model)){
            return $model;
        }
        $userModel = new UserRepository();
        $user = $userModel->getShowOne($model->cid);
        $model->uname = $user ? $user->name : '测试用户';
        $model->head = $user ? $user->head : UserModel::defaultHead;
        $model->head= Fun::pImg($model->head);
        $model->intro = $model->content;
        if (mb_strlen($model->content)>20) {
            $model->intro = mb_substr($model->content,0,20).'...';
        }
        $model['time']=Fun::date($model->created_at);
        return $model;
    }

    /**
     * 资讯点赞
     */
    public function setPraise($id,$uid)
    {
        //目前没有取消点赞，后期如果有这里还得判断是否取消点赞  TODO
        if($this->newsPriseModel->where(['news_id'=>$id,'uid'=>$uid])->first()){
            return ['status'=>false,'msg'=>'已经点赞过了'];
        }
        $model = $this->newsModel->where('id',$id)->first();
        if (!$model) {
            return ['status'=>false,'msg'=>'数据不存在'];
        }

        if($this->newsPriseModel->insert([
            'news_id'=>$id,
            'uid'=>$uid,
            'gmt_create'=>time(),
            'gmt_modify'=>time()
        ])){
            $praise = $model->praise + 1;
            $this->newsModel
                ->where('id',$id)
                ->update(array('praise'=>$praise));
//           ->increment('praise',1);
            return ['status'=>true,'msg'=>'点赞成功','data'=>$praise];
        }else{
            return ['status'=>false,'msg'=>'操作失败'];
        }
    }

    /**
     * 添加资讯
     */
    public function addNew($data)
    {
        return $this->newsModel->insert($data);
    }

    /**
     * 更新资讯
     */
    public function updateNew($id,$data)
    {
        return $this->newsModel->where('id',$id)->update($data);
    }

}